package com.binary.coder.gift.core.infrastructure.repository.bill;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.binary.coder.gift.core.common.bean.PageBean;
import com.binary.coder.gift.core.common.constant.AccountType;
import com.binary.coder.gift.core.domain.bill.model.entity.AccountBookEntity;
import com.binary.coder.gift.core.domain.bill.model.entity.AccountRecordEntity;
import com.binary.coder.gift.core.domain.bill.repository.AccountRepository;
import com.binary.coder.gift.core.infrastructure.mapper.bill.AccountBookMapper;
import com.binary.coder.gift.core.infrastructure.mapper.bill.AccountRecordMapper;
import com.binary.coder.gift.core.infrastructure.po.bill.AccountBook;
import com.binary.coder.gift.core.infrastructure.po.bill.AccountRecord;
import com.binary.coder.gift.core.interfaces.bill.dto.req.PageQueryAccountBookReq;
import com.binary.coder.gift.core.interfaces.bill.dto.req.PageQueryAccountRecordReq;
import com.binary.coder.gift.core.interfaces.bill.dto.res.PageQueryAccountBookRes;
import com.binary.coder.gift.core.interfaces.bill.dto.res.PageQueryAccountRecordRes;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.stream.Collectors;


/**
 * @author weitao
 */
@Repository
public class AccountRepositoryImpl implements AccountRepository {

    @Resource
    private AccountBookMapper accountBookMapper;

    @Resource
    private AccountRecordMapper accountRecordMapper;

    @Override
    public void saveAccountRecord(AccountRecordEntity entity) {
        AccountRecord accountRecord = BeanUtil.copyProperties(entity, AccountRecord.class);
        int count = accountRecordMapper.updateById(accountRecord);
        if (count <= 0) {
            accountRecordMapper.insert(accountRecord);
        }
    }

    @Override
    public PageBean<PageQueryAccountRecordRes> pageQueryAccountRecord(PageQueryAccountRecordReq req) {
        LambdaQueryWrapper<AccountRecord> queryWrapper = Wrappers.lambdaQuery(AccountRecord.class);
        // 随礼
        if (AccountType.PAYEE.getTypeCode().equals(req.getAccountType())) {
            queryWrapper = queryWrapper.eq(AccountRecord::getPayerId,req.getUserId());
            if (StringUtils.isNotEmpty(req.getName())) {
                queryWrapper = queryWrapper.like(AccountRecord::getPayeeName,req.getName());
            }
        }
        // 收礼
        if (AccountType.PAYER.getTypeCode().equals(req.getAccountType())) {
            queryWrapper = queryWrapper.eq(AccountRecord::getPayeeId,req.getUserId());
            if (StringUtils.isNotEmpty(req.getName())) {
                queryWrapper = queryWrapper.like(AccountRecord::getPayerName,req.getName());
            }
        }
        // 条件查询
        if (StringUtils.isNotEmpty(req.getPhone())) {
            queryWrapper = queryWrapper.like(AccountRecord::getPhone,req.getPhone());
        }
        if (StringUtils.isNotEmpty(req.getAddress())) {
            queryWrapper = queryWrapper.like(AccountRecord::getAddress,req.getAddress());
        }
        // 排序
        if (Objects.nonNull(req.getAmountDesc())) {
            queryWrapper = req.getAmountDesc() ? queryWrapper.orderByDesc(AccountRecord::getAmount) : queryWrapper.orderByAsc(AccountRecord::getAmount);
        }
        if (Objects.nonNull(req.getDateOfGiftDesc())) {
            queryWrapper = req.getDateOfGiftDesc() ? queryWrapper.orderByDesc(AccountRecord::getDateOfGift) : queryWrapper.orderByAsc(AccountRecord::getDateOfGift);
        }
        // 分页查询
        Page<AccountRecord> accountRecordPage = accountRecordMapper.selectPage(new Page<>(req.getPageIndex(), req.getPageSize()), queryWrapper);
        // 赋值转换
        PageBean<PageQueryAccountRecordRes> res = new PageBean<>(accountRecordPage.getCurrent(),accountRecordPage.getSize(),accountRecordPage.getTotal());
        res.setRecords(accountRecordPage.getRecords().stream().map(e->BeanUtil.copyProperties(e,PageQueryAccountRecordRes.class)).collect(Collectors.toList()));
        return res;
    }

    @Override
    public void saveAccountBook(AccountBookEntity accountBookEntity) {
        AccountBook accountBook = BeanUtil.copyProperties(accountBookEntity, AccountBook.class);
        int count = accountBookMapper.updateById(accountBook);
        if (count <= 0) {
            accountBookMapper.insert(accountBook);
        }
    }

    @Override
    public PageBean<PageQueryAccountBookRes> pageQueryAccountBook(PageQueryAccountBookReq req) {
        LambdaQueryWrapper<AccountBook> queryWrapper = Wrappers.lambdaQuery(AccountBook.class).eq(AccountBook::getCreateUserId,req.getUserId());
        if (Objects.nonNull(req.getBookType())) {
            queryWrapper = queryWrapper.eq(AccountBook::getBookType,req.getBookType());
        }
        if (Objects.nonNull(req.getEventDateDesc())) {
            queryWrapper = req.getEventDateDesc() ? queryWrapper.orderByDesc(AccountBook::getEventDate) : queryWrapper.orderByAsc(AccountBook::getEventDate);
        }
        Page<AccountBook> accountBookPage = accountBookMapper.selectPage(new Page<>(req.getPageIndex(), req.getPageSize()), queryWrapper);
        // 赋值转换
        PageBean<PageQueryAccountBookRes> res = new PageBean<>(accountBookPage.getCurrent(),accountBookPage.getSize(),accountBookPage.getTotal());
        res.setRecords(accountBookPage.getRecords().stream().map(e->BeanUtil.copyProperties(e,PageQueryAccountBookRes.class)).collect(Collectors.toList()));
        return res;
    }
}
